﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ClickToCall.CustomControls
{
    public partial class Button : System.Windows.Forms.Control
    {
        Bitmap button, buttonHighlight;
        STATE state = STATE.NORMAL;
        bool isHover = false;
        public Button()
        {
            InitializeComponent();
            button = new Bitmap(
                Properties.Resources.button1, 
                new Size(
                    Properties.Resources.button1.Width, 
                    Properties.Resources.button1.Height
                    )
                );
            buttonHighlight = new Bitmap(
                Properties.Resources.buttonhighlight1, 
                new Size(
                    Properties.Resources.buttonhighlight1.Width, 
                    Properties.Resources.buttonhighlight1.Height
                    )
                );
            OnResize(new EventArgs());
        }

        private DialogResult _dialogResult = DialogResult.None;

        public DialogResult DialogResult
        {
            get { return this._dialogResult; }
            set { this._dialogResult = value; }
        }

        protected override void OnResize(EventArgs e)
        {
            this.Size = new Size(74, 20);
            base.OnResize(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.state = STATE.PRESSED;
            this.Invalidate();
            base.OnMouseDown(e);
        }
        protected override void OnMouseEnter(EventArgs e)
        {
            this.state = STATE.HOVER;
            this.isHover = true;
            this.Invalidate();
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            this.state = STATE.NORMAL;
            this.isHover = false;
            this.Invalidate();
            base.OnMouseLeave(e);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if(this.isHover)
                this.state = STATE.HOVER;
            else
                this.state = STATE.NORMAL;
            this.Invalidate();
            base.OnMouseUp(e);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            Graphics g = pe.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            
            if (this.state == STATE.HOVER)
            {
                g.DrawImage(buttonHighlight, 0, 0);
            }
            else if (this.state == STATE.PRESSED)
            {
                g.DrawImage(button, 0, 0);
                g.DrawLine(new Pen(Brushes.Gray), 0, this.Height, this.Height, this.Width); 
            }
            else
            {
                g.DrawImage(button, 0, 0);
            }

            System.Drawing.Drawing2D.GraphicsPath path =
                new System.Drawing.Drawing2D.GraphicsPath();
            path.AddLines(
                    new Point[]{
                        new Point(0,2),
                        new Point(0,button.Height - 2),
                        new Point(1,button.Height - 2),
                        new Point(1,button.Height - 1),
                        new Point(2,button.Height - 1),
                        new Point(2,button.Height),
                        new Point(button.Width-2,button.Height),
                        new Point(button.Width-2,button.Height-1),
                        new Point(button.Width-1,button.Height-1),
                        new Point(button.Width-1,button.Height-2),
                        new Point(button.Width,button.Height-2),
                        new Point(button.Width,2),
                        new Point(button.Width-1,2),
                        new Point(button.Width-1,1),
                        new Point(button.Width-2,1),
                        new Point(button.Width-2,0),
                        new Point(2,0),
                        new Point(2,1),
                        new Point(1,1),
                        new Point(1,2),
                        new Point(0,2),
                    }
                );
            this.Region = new Region(path);

            if (this.Text.Length > 0)
            {
                SizeF sz = g.MeasureString(this.Text, this.Font);
                g.DrawString( this.Text,
                              this.Font,
                              new SolidBrush(this.ForeColor),
                              (this.ClientSize.Width - sz.Width) / 2,
                              (this.ClientSize.Height - (sz.Height)) / 2
                             );
            }
            base.OnPaint(pe);
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            //base.OnPaintBackground(pevent);
        }
    }

    enum STATE
    {
        NORMAL,
        HOVER,
        PRESSED
    }
}
